package com.neu.cloudFactory.controller;

import com.neu.cloudFactory.PO.Factory;
import com.neu.cloudFactory.VO.FactoryVO;
import com.neu.cloudFactory.service.FactoryService;
import com.neu.cloudFactory.utils.result.Result;
import com.neu.cloudFactory.utils.result.ResultFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/factory")
public class FactoryController {

    final FactoryService factoryService;

    public FactoryController(FactoryService factoryService) {
        this.factoryService = factoryService;
    }

    @CrossOrigin
    @GetMapping(value = "/getAllFactory")
    @ResponseBody
    //列出所有工厂
    public Result getAllFactory(){
        List<FactoryVO> factoryVOList = factoryService.findAllFactoryVO();
        return ResultFactory.buildSuccessResult(factoryVOList);
    }
    //开启工厂
    @CrossOrigin
    @PostMapping(value = "/openFactory")
    @ResponseBody
    public Result openFactory(@RequestBody Factory responseFactory ){
        factoryService.openFactory(responseFactory);
        return ResultFactory.buildSuccessResult("开启成功");
    }

    //关闭工厂
    @CrossOrigin
    @PostMapping(value = "/closeFactory")
    @ResponseBody
    public Result closeFactory(@RequestBody Factory responseFactory ){
        factoryService.closeFactory(responseFactory);
        return ResultFactory.buildSuccessResult("关闭成功");
    }

    //编辑
    @CrossOrigin
    @PostMapping(value = "/modifyFactory")
    @ResponseBody
    public Result modifyFactory(@RequestBody FactoryVO responseFactoryVO ){
        Factory factory = factoryService.changeType(responseFactoryVO);
        factoryService.modifyFactory(factory);
        return ResultFactory.buildSuccessResult("修改成功");
    }
}
